<?php
header('Content-type:text/html;charset=utf-8');
class CardAction extends Action{
	function bankInfo($card,$bankList){//【检查银行卡归属】

		$card_8 = substr($card, 0, 8);
		if (isset($bankList[$card_8])) {
			return $bankList[$card_8];
        }
    	$card_6 = substr($card, 0, 6);
    	if (isset($bankList[$card_6])) {
        	return $bankList[$card_6];
        }
    	$card_5 = substr($card, 0, 5);
    	if (isset($bankList[$card_5])) {
        	return $bankList[$card_5];
       }
    	$card_4 = substr($card, 0, 4);
    	if (isset($bankList[$card_4])) {
        	return $bankList[$card_4];
        }
    	return false;
}
 

	public function addCard(){//【添加/更新银行卡信息】
		
		$bankList = model('Bank')->getBanklist();
		$user_card = M('User_card');
		$user_card->uid = $uid = dt($_REQUEST['uid']);
		$cardnum = str_replace(" ", "",str_replace(" ", "", dt($_REQUEST['cardnum'])));
		$user_card->name = $name = dt($_REQUEST['name']);
		if(empty($uid) || empty($cardnum) || empty($name)){
		
			$value = ["code" =>"empty"];
			header("Content-Type: text/json");
			die(et($value));
		}
		$is_card = $this->bankInfo($cardnum, $bankList);
		if(!$is_card){//银行卡号不合法
			$value = ["code" =>"cardfalse"];
			header("Content-Type: text/json");
			die(et($value));
		}
		//echo $is_card;exit;
		$user_card->card_name = $car_name = $is_card;
		$user_card->card_num = $car_num = base64_encode($cardnum);
		$find = $user_card->where("uid = $uid")->find();
		if($find){
			$data['name'] = $name;
			$data['card_num'] = $car_num;
			$data['card_name'] = $car_name;
			if($user_card->where("uid = $uid")->data($data)->save()){
				$data = M('User_card')->where("uid = $uid")->find();
				$value = ["code" =>"success",'data'=>$data];
				header("Content-Type: text/json");
				die(et($value));
			}else{
				$value = ["code" =>"false"];
				header("Content-Type: text/json");
				die(et($value));
			}
		}
		
		//dump($user_card);exit;
		if($user_card->add()){
			$data = M('User_card')->where("uid = $uid")->find();
			$value = ["code" =>"success",'data'=>$data];
			header("Content-Type: text/json");
			die(et($value));
		}else{
			$value = ["code" =>"false"];
			header("Content-Type: text/json");
			die(et($value));
		}
		
	}
	
	public function getCard(){//【获取银行卡信息】
		
		$uid = dt($_REQUEST['uid']);
		$user_card = M('User_card');
		$find = $user_card->where("uid = $uid")->find();
		if($find){
			
			$value = ["code" =>"success",'data'=>$find];
			header("Content-Type: text/json");
			die(et($value));
		}else{
			$user = M('User')->where("uid = $uid")->find();
			if($user){
				$value = ["code" =>"success",'data'=>array()];
				header("Content-Type: text/json");
				die(et($value));
			}else{
				$value = ["code" =>"false"];
				header("Content-Type: text/json");
				die(et($value));
			
			}
		
		}
	
	}

	

}